package com.xiaoxing.salesclient.mvp.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xiaoxing.salesclient.di.component.DaggerChanPinJiaGeComponent;
import com.xiaoxing.salesclient.di.module.ChanPinJiaGeModule;
import com.xiaoxing.salesclient.mvp.contract.ChanPinJiaGeContract;
import com.xiaoxing.salesclient.mvp.model.entity.ProductCateList;
import com.xiaoxing.salesclient.mvp.model.entity.ProductList;
import com.xiaoxing.salesclient.mvp.presenter.ChanPinJiaGePresenter;
import com.xiaoxing.salesclient.mvp.ui.adapter.ChanPinJiaGeTabAdapter;
import com.xiaoxing.salesclient.mvp.ui.entity.TabEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;
import me.jessyan.armscomponent.commonsdk.core.BaseConstants;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;
import xiaoxing.com.salesclient.R;
import xiaoxing.com.salesclient.R2;

import static com.jess.arms.utils.Preconditions.checkNotNull;


@Route(path = RouterHub.SALES_CLIENT_CHAN_PIN_JIA_GE_ACTIVITY)
public class ChanPinJiaGeActivity extends BaseActivity<ChanPinJiaGePresenter> implements ChanPinJiaGeContract.View, OnRefreshListener {

    @BindView(R2.id.tv_chan_pin_jia_mu_biao)
    TextView tv_chan_pin_jia_mu_biao;
    @BindView(R2.id.tv_liao_cheng_jia_mu_biao)
    TextView tv_liao_cheng_jia_mu_biao;

    private SlidingTabLayout mSlidingTabLayout;
    private List<ProductCateList.DataBean> dataBeanList = new ArrayList<>();

    @BindView(R2.id.tl_3)
    CommonTabLayout mCommonTabLayout;


    TextView empty_text;

    ImageView empty_image;

    View mEmptyLayout;

    RecyclerView mRecyclerView;

    RefreshLayout mRefreshLayout;
    private ChanPinJiaGeTabAdapter mAdapter;
    private String mTypeId = "";
    private List<ProductList.DataBean> mDataBeanList = new ArrayList<>();
    private int type = 0;

    private int[] mIconUnselectIds = {
            R.mipmap.tab_home_unselect, R.mipmap.tab_speech_unselect,
            R.mipmap.tab_contact_unselect, R.mipmap.tab_more_unselect};
    private int[] mIconSelectIds = {
            R.mipmap.tab_home_select, R.mipmap.tab_speech_select,
            R.mipmap.tab_contact_select, R.mipmap.tab_more_select, R.mipmap.tab_more_select, R.mipmap.tab_more_select};

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerChanPinJiaGeComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .chanPinJiaGeModule(new ChanPinJiaGeModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_chan_pin_jia_ge; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        ToolbarUtils.initToolbarTitleBack(this, "療程及產品價目表");


        mRefreshLayout = (RefreshLayout) findViewById(R.id.refreshLayout);
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mEmptyLayout = (View) findViewById(R.id.empty);
        empty_text = (TextView) findViewById(R.id.empty_text);
        empty_image = (ImageView) findViewById(R.id.empty_image);

        initRefreshLayout();
        initRecyclerView();
        initEmpty();


        mPresenter.productCateList();

    }

    private void initRefreshLayout() {
        mRefreshLayout.setRefreshHeader(new ClassicsHeader(this).setSpinnerStyle(SpinnerStyle.FixedBehind).setPrimaryColorId(R.color.public_colorPrimary).setAccentColorId(android.R.color.white));
        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.autoRefresh();
        mRefreshLayout.setEnableLoadMore(false);
    }

    private void initRecyclerView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, VERTICAL));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(mAdapter = new ChanPinJiaGeTabAdapter(this, mDataBeanList));

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                Utils.navigation(this, RouterHub.SALES_CLIENT_ZUI_XIN_XIAO_XI_XIANG_QING_ACTIVITY);
            }
        });
    }

    private void initEmpty() {
        empty_image.setImageResource(R.drawable.no_data);
        empty_text.setTextColor(getResources().getColor(R.color.public_no_data));
        empty_text.setText("暫無消息,下拉更新");
    }

    @OnClick({R2.id.tv_chan_pin_jia_mu_biao, R2.id.tv_liao_cheng_jia_mu_biao})
    void onClick(View view) {
        int id = view.getId();
        clearData();
        if (id == R.id.tv_chan_pin_jia_mu_biao) {
            mCommonTabLayout.setVisibility(View.VISIBLE);
            type = 0;
            tv_liao_cheng_jia_mu_biao.setTextColor(Color.parseColor("#999999"));
            tv_chan_pin_jia_mu_biao.setTextColor(getResources().getColor(R.color.public_black));
            if (mCommonTabLayout != null && dataBeanList != null && dataBeanList.size() > 0) {
                getProductList();
            }
//                ChanPinJiaGeTabFragment.mJiaMu.chanPinJiaMu(dataBeanList.get(mSlidingTabLayout.getCurrentTab()).getType_id());

        } else if (id == R.id.tv_liao_cheng_jia_mu_biao) {
            mCommonTabLayout.setVisibility(View.GONE);
            type = 1;
            tv_liao_cheng_jia_mu_biao.setTextColor(getResources().getColor(R.color.public_black));
            tv_chan_pin_jia_mu_biao.setTextColor(Color.parseColor("#999999"));
            if (mCommonTabLayout != null && dataBeanList != null && dataBeanList.size() > 0)
//                ChanPinJiaGeTabFragment.mJiaMu.liaoChengJiaMU(dataBeanList.get(mSlidingTabLayout.getCurrentTab()).getType_id());
            {
                getMedproList();
            }
        }

    }

    private void clearData() {
        mDataBeanList.clear();
        mAdapter.notifyDataSetChanged();
        mEmptyLayout.setVisibility(View.VISIBLE);
    }

    private void getMedproList() {
        if (dataBeanList != null && dataBeanList.size() > 0) {
            mPresenter.medproList(dataBeanList.get(mCommonTabLayout.getCurrentTab()).getType_id(), mSharedPreferencesHelper.getString(BaseConstants.SHOP_ID));
        }
    }

    private void getProductList() {
        if (dataBeanList != null && dataBeanList.size() > 0) {
            mPresenter.productList(dataBeanList.get(mCommonTabLayout.getCurrentTab()).getType_id(), mSharedPreferencesHelper.getString(BaseConstants.SHOP_ID));
        }
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void productCateListSuccessed(ProductCateList productCateList) {

        if (productCateList != null && productCateList.getData() != null && productCateList.getData().size() > 0) {

            dataBeanList.addAll(productCateList.getData());

            if (dataBeanList != null && dataBeanList.size() > 0) {

                ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
                ArrayList<Fragment> mFragments = new ArrayList<>();
                String[] mTitles = new String[dataBeanList.size()];

                for (int i = 0; i < dataBeanList.size(); i++) {
                    mTitles[i] = dataBeanList.get(i).getType_name();
//                mFragments.add(ChanPinJiaGeTabFragment.newInstance(dataBeanList.get(i).getType_id()));
                    mTabEntities.add(new TabEntity(mTitles[i], R.mipmap.tab_home_select, R.mipmap.tab_home_unselect));
                    mCommonTabLayout.setTabData(mTabEntities);
                }
                mCommonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
                    @Override
                    public void onTabSelect(int position) {
                        clearData();
                        getListData();
                    }

                    @Override
                    public void onTabReselect(int position) {

                    }
                });
//            mSlidingTabLayout = SlidingTabLayoutUtil.init(this, mTitles, mFragments);
            }
        }

    }


    @Override
    public void productListSuccessed(ProductList productList) {

        if (productList.getData() != null && productList.getData().size() > 0) {
            mEmptyLayout.setVisibility(View.GONE);
            mRecyclerView.setVisibility(View.VISIBLE);
            mDataBeanList.clear();
            mDataBeanList.addAll(productList.getData());
            mAdapter.notifyDataSetChanged();
        } else {
            mEmptyLayout.setVisibility(View.VISIBLE);
            mRecyclerView.setVisibility(View.GONE);
        }

    }

    @Override
    public void medproListSuccessed(ProductList productList) {

        if (productList.getData() != null && productList.getData().size() > 0) {
            mEmptyLayout.setVisibility(View.GONE);
            mRecyclerView.setVisibility(View.VISIBLE);
            mDataBeanList.clear();
            mDataBeanList.addAll(productList.getData());
            mAdapter.notifyDataSetChanged();
        } else {
            mEmptyLayout.setVisibility(View.VISIBLE);
            mRecyclerView.setVisibility(View.GONE);

        }
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        getListData();
        mRefreshLayout.finishRefresh();
    }

    private void getListData() {
        if (type == 0) {
            getProductList();
        } else {
            getMedproList();
        }
    }
}
